package cn.lg.soar.system.biz.modules.auth.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.MenuDTO;
import cn.lg.soar.system.api.service.IMenuApi;
import cn.lg.soar.system.biz.modules.auth.entity.Menu;
import cn.lg.soar.system.biz.modules.auth.service.IMenuService;
import cn.lg.soar.system.biz.modules.auth.service.RoleMenuRelService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/menu")
public class MenuApiClient implements IMenuApi {

    @Autowired
    private IMenuService service;
    @Autowired
    private RoleMenuRelService roleMenuRelService;

    public List<Integer> idsByRole(@RequestParam(name = "roleId") Long roleId) {
        return roleMenuRelService.getMenuIds(roleId);
    }

    @GetMapping("listByIds")
    @Override
    public List<MenuDTO> listByIds(@RequestParam(name = "ids") List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return service.listByIds(ids).stream().map(this::toDto).collect(Collectors.toList());
    }

    @GetMapping("getById")
    @Override
    public MenuDTO getById(@RequestParam(name = "id") Integer id) {
        return toDto(service.getById(id));
    }

    private MenuDTO toDto(Menu menu) {
        MenuDTO dto = new MenuDTO();
        dto.setId(menu.getId());
        dto.setTitle(menu.getTitle());
        dto.setParentId(menu.getParentId());
        dto.setCode(menu.getCode());
        dto.setSort(menu.getSort());
        dto.setIcon(menu.getIcon());
        dto.setHideChildren(menu.getHideChildren());
        dto.setType(menu.getType());
        return dto;
    }

}
